const int FORWARD = 1 << 0;
const int BACK    = 1 << 1;
const int LEFT    = 1 << 2;
const int RIGHT   = 1 << 3;

// pins
const int forward_pin =  10;
const int back_pin = 9;
const int left_pin = 11;
const int right_pin = 12  ;

int forward_state = LOW;
int back_state = LOW;
int left_state = LOW;
int right_state = LOW;

void setup() 
{
  // set the digital pin as output:
  pinMode(forward_pin, OUTPUT);      
  pinMode(back_pin, OUTPUT);
  pinMode(left_pin, OUTPUT);
  pinMode(right_pin, OUTPUT);
  
  Serial.begin(9600);
}

void loop()
{ 
  if (Serial.available() > 0) 
  {
    int in = Serial.read();
    Serial.flush();

    forward_state = in & FORWARD;
    back_state    = in & BACK;
    left_state    = in & LEFT;
    right_state   = in & RIGHT;
  }
  
  digitalWrite(forward_pin, forward_state);
  digitalWrite(back_pin, back_state);
  digitalWrite(left_pin, left_state);
  digitalWrite(right_pin, right_state);
}

